package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.atguigu.tingshu.vo.user.UserSubscribeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 根据用户ID查询用户信息,用于openfeign远程调用
	 * @param userId
	 * @return
	 */
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	@Operation(summary = "根据用户ID查询用户信息")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable(name = "userId") Long userId){
		UserInfoVo userInfoVo = userInfoService.getUserInfoVo(userId);

		return Result.ok(userInfoVo);
	}


	/**
	 * 提交需要检查购买状态声音ID列表，响应每个声音购买状态
	 * @param userId
	 * @param albumId
	 * @param needCheckPayStatusTrackIdList 待检查购买状态声音ID列表
	 * @return
	 */
	@Operation(summary = "获取用户声音列表付费情况")
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long,Integer>> userIsPaidTrack(
			@PathVariable(value = "userId") Long userId,
			@PathVariable(value = "albumId") Long albumId,
			@RequestBody List<Long> needCheckPayStatusTrackIdList
	){

		Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,needCheckPayStatusTrackIdList);

		return Result.ok(map);
	}


	/**
	 * 用户订阅接口
	 * @param albumId
	 * @return
	 */
	@GuiguLogin
	@Operation(summary = "用户订阅接口")
	@GetMapping("/userInfo/subscribe/{albumId}")
	public Result<Boolean> subscribe(@PathVariable(value = "albumId") Long albumId){

		Long userId = AuthContextHolder.getUserId();

		Boolean result = userInfoService.subscribe(albumId,userId);

		return Result.ok(result);
	}


	/**
	 * 判断用户是否购买过指定专辑
	 * @param albumId
	 * @return
	 */
	@Operation(summary = "判断用户是否购买过指定专辑")
	@GuiguLogin
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	public Result<Boolean>  isPaidAlbum(@PathVariable(value = "albumId") Long albumId){

		Long userId = AuthContextHolder.getUserId();

		boolean flag = userInfoService.isPaidAlbum(albumId,userId);

		return Result.ok(flag);
	}

	/**
	 * 查询当前用户指定专辑下已购声音ID列表
	 * @param albumId
	 * @return
	 */
	@GuiguLogin
	@Operation(summary = "查询当前用户指定专辑下已购声音ID列表")
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	public Result<List<Long> > findUserPaidTrackList(@PathVariable Long albumId) {
		Long userId = AuthContextHolder.getUserId();
		List<Long> list = userInfoService.findUserPaidTrackList(userId, albumId);
		return Result.ok(list);
	}


	/**
	 * 新增用户购买记录（声音、专辑、VIP）____虚拟物品发货接口
	 * @param userPaidRecordVo
	 * @return
	 */
	@Operation(summary = "新增用户购买记录（声音、专辑、VIP）____虚拟物品发货接口")
	@GuiguLogin
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){


		userInfoService.savePaidRecord(userPaidRecordVo);


		return Result.ok();
	}


	/**
	 * 获取用户是否订阅
	 * @param albumId
	 * @return
	 */
	@Operation(summary = "获取用户是否订阅")
	@GuiguLogin(required = false)
	@GetMapping("/userInfo/isSubscribe/{albumId}")
	public Result<Boolean> isSubscribe(@PathVariable(value = "albumId") Long albumId){

		Long userId = AuthContextHolder.getUserId();

		Boolean result = userInfoService.isSubscribe(albumId,userId);

		return Result.ok(result);
	}


	/**
	 * 获取用户订阅分页列表
	 * @param page
	 * @param limit
	 * @return
	 */
	@Operation(summary = "获取用户订阅分页列表")
	@GuiguLogin(required = false)
	@GetMapping("/userInfo/findUserSubscribePage/{page}/{limit}")

	public Result<Page<UserSubscribeVo>> findUserSubscribePage(
			@PathVariable(value = "page") Long page,
			@PathVariable(value = "limit") Long limit){

		Long userId = AuthContextHolder.getUserId();

		Page<UserSubscribeVo> page1 = new Page<>(page,limit);

		page1 = userInfoService.findUserSubscribePage(page1,userId);

		return Result.ok(page1);
	}

}

